package top.fengleifeng.question;

import com.alibaba.fastjson.JSONObject;

/**
 * @author : fengleifeng
 * @Package top.fengleifeng.question
 * @Description:
 * @date Date : 2020年12月17日 5:12 下午
 */
public class Num303区域和检索_数组不可变 {

//    class NumArray {
//
//        int[][] dp = null;
//
//        public NumArray(int[] nums) {
//            if(nums.length<=0){
//                dp = null;
//                return;
//            }
//            dp = new int[nums.length][nums.length];
//            dp[0][0] = nums[0];
//            for (int i = 0; i < nums.length; i++) {
//                dp[i][i] = nums[i];
//                for (int j = i + 1; j < nums.length; j++) {
//                    dp[i][j] = dp[i][j - 1] + nums[j];
//                }
//            }
//        }
//
//        public int sumRange(int i, int j) {
//            if(null==dp){
//                return 0;
//            }
//            return dp[i][j];
//        }
//    }


    class NumArray {

        int[] dp = null;

        public NumArray(int[] nums) {
            if (nums.length <= 0) {
                return;
            }
            dp = new int[nums.length];
            dp[0] = nums[0];
            for (int i = 1; i < nums.length; i++) {
                dp[i] = dp[i - 1] + nums[i];
            }
        }

        public int sumRange(int i, int j) {
            if (null == dp) {
                return 0;
            }

            return dp[j] - (i > 0 ? dp[i - 1] : 0);
        }
    }

    /**
     * Your NumArray object will be instantiated and called as such: NumArray obj = new NumArray(nums); int param_1 =
     * obj.sumRange(i,j);
     */
    public static void main(String[] args) {
//        ["NumArray","sumRange","sumRange","sumRange"]
//[[[-2,0,3,-5,2,-1]],[0,2],[2,5],[0,5]]
        int[] nums = {-2, 0, 3, -5, 2, -1};
        NumArray numArray = new Num303区域和检索_数组不可变().new NumArray(nums);
        System.out.println(JSONObject.toJSONString(numArray));
    }
}
